<?php
class MantenedorChecklist
{
	public function ListarCheckList($id)
		{
			$select_id=0;
            $desactivo = 0;
			
			echo "<table>";
			require_once("../include/conec.php");
			$link = Conectarse();
			$result = mysql_query("SELECT id_parametro,valor FROM parametros where tipo='DOCUMENTOSCLIENTE' and estado=1", $link);
			while($row=mysql_fetch_array($result)){

				echo "<tr>";
				echo "<td width='5%' align='center'><input type='checkbox' name='seleccion[]' value='".$row['id_parametro']."'";
				
				//falta recorrer tabla reservas_checklist con el id_reserva sacar el id_documento en $select_id para comprobar
                $result2 = mysql_query("SELECT id_documento,`check` as desactivo FROM reservas_checkpreaprobacion where id_documento='".$row['id_parametro']."' and id_reserva='".$id."'", $link);
				while($row2=mysql_fetch_array($result2)){
                 $select_id =  $row2['id_documento'];
                 $desactivo =  $row2['desactivo'];
                }
                if($result2!="") mysql_free_result($result2);
                
				if($select_id == $row['id_parametro'])
					echo " checked";
                if($desactivo == 1)
				    echo " disabled";
				echo ">";
				echo "</td>";
				
				echo "<td>";
				echo reemplaza_caracteres($row['valor']);
								
				echo ".</td></tr>";
				
				$select_id = 0;
                $desactivo = 0;
				
			}
			echo "</table>";

			if($result!="")	mysql_free_result($result);
			mysql_close($link);
	}
    public function ListarCheckear_Doc($id)
		{
			$select_id=0;
			
			echo "<table>";
			require_once("../include/conec.php");
			$link = Conectarse();
			$result = mysql_query("SELECT rc.id, rc.id_reserva, rc.id_documento, p.valor,c.rut_cliente, rc.id_clientedocumento
                                    FROM reservas_checkpreaprobacion rc
                                    INNER JOIN parametros p ON ( rc.id_documento = p.id_parametro And p.tipo ='DOCUMENTOSCLIENTE')
                                    INNER JOIN reservas r ON (r.id = rc.id_reserva)
                                    INNER JOIN cotizaciones c ON (c.id = r.id_cotizacion) 
                                    WHERE rc.id_reserva ='".$id."'", $link);
			while($row=mysql_fetch_array($result)){
                $select_id = $row['id_clientedocumento'];
				echo "<tr>";
				//echo "<td width='5%' align='center'><input type='checkbox' name='seleccion[]' value='".$row['id_parametro']."'";
				//echo "<td width='5%' align='center'>";
				//falta recorrer tabla reservas_checklist con el id_reserva sacar el id_documento en $select_id para comprobar
                
                echo "<td>";
				echo reemplaza_caracteres($row['valor']);
				echo ".</td>";
                
                echo "<td>";
                echo "<select name='cmb_documentos[]'>";
                echo "<option value='".$row['id']."|null'></option>";
                $result2 = mysql_query("SELECT idclientes_documentos as id, comentario FROM clientes_documentos where rut_cliente='".$row['rut_cliente']."'", $link);
				while($row2=mysql_fetch_array($result2)){
                 if ($select_id == $row2['id']){
                    echo "<option value='".$row['id']."|".$row2['id']."' selected>".$row2['comentario']."</option>";
                 }else{
                    echo "<option value='".$row['id']."|".$row2['id']."'>".$row2['comentario']."</option>";
                 }
                }
                if($result2!="") mysql_free_result($result2);
				echo "</select>";
				echo "</td>";
                echo "</tr>";
				
				$select_id = 0;
				
			}
			echo "</table>";

			if($result!="")	mysql_free_result($result);
			mysql_close($link);
	}
    public function Guardar_Asociacion_Check_Documento($id_reserva,$id_documento,$id_clientedocumento)
	{

		require_once("../include/conec.php");
		$link = Conectarse();
        
		$query = "UPDATE 
                    `reservas_checkpreaprobacion` 
                    SET id_clientedocumento ='".$id_clientedocumento."',
                        `check`=1  
                    WHERE id_reserva='".$id_reserva."' and id='".$id_documento."';";
		//Run Query
		$result= mysql_query($query,$link);
		//Return message based on the result
		if ($result == 1)
		{
			mysql_close($link);
			return("1");

		}
		else
		{
			mysql_close($link);
			return("0");
		}


		mysql_close($link);
	}
	public function GuardarChecklist($id_reserva,$id_documento,$fecha_check,$usuario_check)
	{

		require_once("../include/conec.php");
		$link = Conectarse();
        
		$query = "INSERT INTO reservas_checkpreaprobacion (id,id_reserva,id_documento,fecha_check,usuario_check) VALUES (0,'$id_reserva','$id_documento','$fecha_check','$usuario_check')";

		//Run Query
		$result= mysql_query($query,$link);
		//Return message based on the result
		if ($result == 1)
		{
			mysql_close($link);
			return("1");

		}
		else
		{
			mysql_close($link);
			return("0");
		}


		mysql_close($link);
	}
    
    Public function EliminaChecklist($id_reserva){
        require_once("../include/conec.php");
		$link = Conectarse();
        $queryElimina = "DELETE FROM reservas_checkpreaprobacion where id_reserva='$id_reserva';";
        $resultElimina = mysql_query($queryElimina,$link);
        if ($resultElimina == 1)
		{
			mysql_close($link);
			return("1");

		}
		else
		{
			mysql_close($link);
			return("0");
		}
    }

}
?>

